<?php
/**--------------------------------------------------------------------------+
 * Project:     CuteView: the PHP template engine                            |
 * File:        CuteViewDynamicCacheEngine.php                               |
 *---------------------------------------------------------------------------+
 * This library is free software; you can redistribute it and/or             |
 * modify it under the terms of the GNU Lesser General Public                |
 * License as published by the Free Software Foundation; either              |
 * version 2.1 of the License, or (at your option) any later version.        |
 *                                                                           |
 * This library is distributed in the hope that it will be useful,           |
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            |
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         |
 * Lesser General Public License for more details.                           |
 *                                                                           |
 * You should have received a copy of the GNU Lesser General Public          |
 * License along with this library; if not, write to the Free Software       |
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA |
 *                                                                           |
 * For questions, help, comments, discussion, etc., please contact the author|
 *---------------------------------------------------------------------------+
 * @copyright 2007-2007 blizzchris                                           |
 * @author Chris Yue <blizzchris@gmail.com>                                  |
 * @package CuteView                                                         |
 * @version 1.071222 beta                                                    |
 *---------------------------------------------------------------------------+
 */

require_once('Base/CuteViewEngine.php');

class CuteViewDynamicCacheEngine extends CuteViewEngine
{
	/**
	 * set true to always delete generated php file
	 * it is useful when designers attempt to change the template file
	 *
	 * @var bool
	 */
	public $always_delete_cache = false;
	
	
	/**---------------------+
	 * private members below|
	 *----------------------*/
	/**
	 * where the generated php files are put in
	 *
	 * @var string
	 */
	private $cache_file_root = 'compiled';
	
	/**
	 * the generated php file's path
	 *
	 * @var string
	 */
	private $cache_file_path;
	
	
	/**
	 * set where to put the generate php files in
	 *
	 * @param string $root_path
	 */
	public function setCacheRootPath($root_path)
	{
		$this -> cache_file_root = rtrim($root_path) . "/";
	}
	
	/**
	 * implement the display method
	 * if the always_delete_cache is not set true, it will generate a
	 * php file and it will remain eternally
	 *
	 */
	public function display()
	{
		$this -> initCacheFilePath();
		
		if ( $this -> always_delete_cache && $this -> cacheFileExists() )
		{
			$this -> deleteCacheFile();
		}

		if ( !$this -> cacheFileExists() )
		{	
			$this -> handleTemplate();
			$this -> generateCacheFile();
		}
		else 
		{
			$this -> loadCacheFile();
		}
		$this -> execute();
	}
	
	/**
	 * initial the generated php file path
	 *
	 * @param string $file_path
	 */
	private function initCacheFilePath()
	{
		$this -> cache_file_path = str_replace("/", "_", $this -> file_path) . ".php";
	}
	
	/**
	 * nothing more to say ...
	 * if you don't like the "Generated comment"
	 * just delete line 90, 91, 92!
	 */
	private function generateCacheFile()
	{
		$time_create = date("Y-m-d H:i:s");
		$this -> content = "<!-- Generated by CuteView on $time_create -->\n{$this -> content}";
		
		file_put_contents("{$this -> cache_file_root}{$this -> cache_file_path}", $this -> content);
	}
	
	/**
	 * check if the generated php file already exists
	 *
	 * @return bool
	 */
	private function cacheFileExists()
	{
		return file_exists("{$this -> cache_file_root}{$this -> cache_file_path}");
	}
	
	/**
	 * just delete the old generated file
	 *
	 */
	private function deleteCacheFile()
	{
		@unlink("{$this -> cache_file_root}{$this -> cache_file_path}")
			or die("Delete cache file {$this -> cache_file_path} failed");
	}
	
	/**
	 * load the generated php file to speed up!
	 * good thought SMARTY!
	 */
	private function loadCacheFile()
	{
		$this -> content = file_get_contents("{$this -> cache_file_root}{$this -> cache_file_path}");
	}
	
}